jQuery(document).ready(function(){
	
	var config = {
		siteURL		: 'humbug.in',	// Change this to your site
		searchSite	: false,
		cseId	: 'partner-pub-8688620973248670:kxgnhxxj5gk',
		type		: 'web',
		append		: false,
		perPage		: 6,			// A maximum of 8 is allowed by Google
		page		: 0				// The start page
	}
	
	
	googleSearch();
	
	
	function googleSearch(settings) {
		
		// If no parameters are supplied to the function,
		// it takes its defaults from the config object above:
		
		settings = jQuery.extend({},config,settings);
		settings.term = 'svn update';
		
		// URL of Google's AJAX search API
		var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&cx='+settings.cseId+'&callback=?';
		var search_results = jQuery('#search_results');
		
		jQuery.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
			
			var results = r.responseData.results;
			jQuery('#more').remove();
			
			if(results.length){
				
				// If results were returned, add them to a pageContainer div,
				// after which append them to the #search_results:
				
				var pageContainer = jQuery('<div>',{className:'pageContainer'});
				
				for(var i=0;i<results.length;i++){
					// Creating a new result object and firing its toString method:
					pageContainer.append(new result(results[i]) + '');
				}
				
				if(!settings.append){
					// This is executed when running a new search, 
					// instead of clicking on the More button:
					search_results.empty();
				}
				
				pageContainer.appendTo(search_results);
				

				var cursor = r.responseData.cursor;
				
				// Checking if there are more pages with results, 
				// and deciding whether to show the More button:
				
				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					jQuery('<div>',{id:'more'}).appendTo(search_results).click(function(){
						googleSearch({append:true,page:settings.page+1});
						jQuery(this).fadeOut();
					});
				}
			}
			else {
				
				// No results were found for this search.
				
				search_results.empty();
				jQuery('<p>',{className:'notFound',html:'No Results Were Found!'}).hide().appendTo(search_results)
			}
		});
	}
	
	function result(r){
		
		// This is class definition. Object of this class are created for
		// each result. The markup is generated by the .toString() method.
		
		var arr = [];
		
		// GsearchResultClass is passed by the google API
		switch(r.GsearchResultClass){

			case 'GwebSearch':
				arr = [
					'<div class="webResult">',
					'<h4><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h4>',
					'<p>',r.content,'</p>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
					'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GvideoSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
					'<img src="',r.tbUrl,'" width="100%" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
			case 'GnewsSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.unescapedUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
		}
		
		// The toString method.
		this.toString = function(){
			return arr.join('');
		}
	}
	
	
});
